$(function(){
	//初始化日期控件
  	$('#beginReg, #endReg, #beginLog, #endLog').datetimepicker({
  		language: 'zh-CN',
	    format: 'yyyy-mm-dd hh:00',
	    autoclose: true,
	    minView: "day"
	});
	
//	$("#searchModal").modal("show");
	
	//加载游戏平台下拉数据
	loadGamePlatform();
	//加载数据
	loadDatas();
	
	//推广号排序
	$("#GameID").click(function(){
		$("#sorts").val($(this).attr("id"));
		initSort($(this));
		changeSortClass($("#GameID span"));
	});
	//资产排序
	$("#score").click(function(){
		$("#sorts").val($(this).attr("id"));
		initSort($(this));
		changeSortClass($("#score span"));
	});
	//奖券排序
	$("#prizes").click(function(){
		$("#sorts").val($(this).attr("id"));
		initSort($(this));
		changeSortClass($("#prizes span"));
	});
	//充值排序
	$("#cz").click(function(){
		$("#sorts").val($(this).attr("id"));
		initSort($(this));
		changeSortClass($("#cz span"));
	});
	//转账排序
	$("#ZengSongCha").click(function(){
		$("#sorts").val($(this).attr("id"));
		initSort($(this));
		changeSortClass($("#ZengSongCha span"));
	});
	//注册时间排序
	$("#RegisterDate").click(function(){
		$("#sorts").val($(this).attr("id"));
		initSort($(this));
		changeSortClass($("#RegisterDate span"));
	});
	//最后登录时间排序
	$("#LastLoginTime").click(function(){
		$("#sorts").val($(this).attr("id"));
		initSort($(this));
		changeSortClass($("#LastLoginTime span"));
	});
	//当日输赢排序
	$("#TodayScore").click(function(){
		$("#sorts").val($(this).attr("id"));
		initSort($(this));
		changeSortClass($("#TodayScore span"));
	});
	//总输赢排序
	$("#RealScore").click(function(){
		$("#sorts").val($(this).attr("id"));
		initSort($(this));
		changeSortClass($("#RealScore span"));
	});
	//用户类型排序
	$("#UserKind").click(function(){
		$("#sorts").val($(this).attr("id"));
		initSort($(this));
		changeSortClass($("#UserKind span"));
	});
});

//加载玩家列表数据
function loadDatas(s){
	
	if(s=="search"){
		$("#page").val("");
	}
	
	//页码
	var page = $("#page").val();
	//一页显示的条数
	var pageSize = 40;
	//游戏平台
	var gamePlatform = $("#gamePlatform").val();
	//控制状态
	var state = $("#state").val();
	//作弊类型
	var zbType = $("#zbType").val();
	//排序
	var sortkey = $("#sorts").val();
	var sortval = "";
	if(sortkey != "" && sortkey != undefined){
		var val = $("#"+sortkey+" span").attr("class");
		if(val.indexOf("asc") != -1){
			sortval = "desc";
		}else if(val.indexOf("desc") != -1){
			sortval = "asc";
		}
	}else{
		sortkey = "";
	}
	
//	alert("sortkey: "+sortkey+"; sortval: "+sortval);
	/*
	 * 搜索框中的条件
	 */
	var uType = $("#uType").val();//搜索项
	var userid = $("#userid").val();//值
	var begin = $("#beginReg").val();//注册时间
	var end = $("#endReg").val();//注册时间
	var beginLog = $("#beginLog").val();//最后登录时间
	var endLog = $("#endLog").val();//最后登录时间
	var minScore = $("#minScore").val();//用户金币
	var maxScore = $("#maxScore").val();//用户金币
	// var minCzScore = $("#minCzScore").val();//充值金额
	// var maxCzScore = $("#maxCzScore").val();//充值金额
	var minPrize = $("#minPrize").val();//奖券
	var maxPrize = $("#maxPrize").val();//奖券
	var minZsy = $("#minZsy").val();//总输赢
	var maxZsy = $("#maxZsy").val();//总输赢
	var minDrsy = $("#minDrsy").val();//当日输赢
	var maxDrsy = $("#maxDrsy").val();//当日输赢
	var minZengSongCha = $("#minZengSongCha").val();//金币赠送差(接收-赠送)
	var maxZengSongCha = $("#maxZengSongCha").val();//金币赠送差(接收-赠送)
	var phone = $("#phone").val();//密保手机
	var loginIp = $("#loginIp").val();//登录IP
	var regIp = $("#regIp").val();//注册IP
	var userState = $("#userState").val();//账号状态
	var usertype = $("#usertype").val();//用户类型
	var platform = $("#platform").val();//游戏平台
	
	$.ajax({
		type: "post",
		data: {
			"page":page, "pageSize":pageSize, "gamePlatform":gamePlatform,
			"state":state, "zbType":zbType,
			"sortKey":sortkey, "sortVal":sortval,
			"uType":uType, "userid":userid,
			"begin":begin, "end":end, 
			"beginLog":beginLog, "endLog":endLog,
			"minScore":minScore, "maxScore":maxScore,
			// "minCzScore":minCzScore, "maxCzScore":maxCzScore,
			"minPrize":minPrize, "maxPrize":maxPrize,
			"minZsy":minZsy, "maxZsy":maxZsy,
			"minDrsy":minDrsy, "maxDrsy":maxDrsy,
			"minZengSongCha":minZengSongCha, "maxZengSongCha":maxZengSongCha,
			"phone":phone, "loginIp":loginIp, "regIp":regIp,
			"userState":userState, "usertype":usertype, "platform":platform
		},
		dateType:'json',
		url: "/player/playerinfo/searchplayerList",
		beforeSend: function(){
			$("#databody").html('<tr><td colspan="18" align="center">数据正在加载中，请稍后~</td></tr>');
			$("#load").css("display","block");
		},
		error:function () {
			alert("error");
		},
		success: function(msg){
			//console.log(msg);
			//return;
		// var msg=eval('(' + msg + ')');
			$("#load").css("display","none");
			//表格数据
			var datas = msg.records;
			//console.log(datas[0].lastLoginTime);
			//return;
			var html = "";
			// alert(datas.length);
			if(datas.length == 0){
				html += "<tr><td colspan='17' align='center'>没查询到符合条件的数据~</td></tr>";
			}else{
				for(var i = 0; i<datas.length; i++){
					//用户类型（0：普通用户；1：内部用户）
					var userKind = datas[i].userKind;
					var typeStr = "";

					var score = datas[i].score;//资产
					var zbl = datas[i].cheatRate//作弊率
					var realScore = datas[i].realScore;//总输赢

					if(userKind == "3"){// 银商 淡黄
						html += "<tr style='background-color:#FFFF00;'>";
						typeStr = "银商";
					}else if(userKind == "2"){//超R 紫色
						html += "<tr style='background-color:#DA12EC;'>";
						typeStr = "超R";
					}else if(userKind == "2" && realScore > 0){// 大R 总输赢大于0
						html += "<tr style='background-color:#eb5858;'>";
						typeStr = "大R";
					}else if(userKind == "2"){// 大R 淡红
						html += "<tr style='background-color:#FF9393;'>";
						typeStr = "大R";
					}else if(userKind == "1" && realScore > 0){// 小R 总输赢大于0
						html += "<tr style='background-color:#00cece;'>";
						typeStr = "小R";
					}else if(userKind == "1"){// 小R 淡蓝
						html += "<tr style='background-color:#00ffff;'>";
						typeStr = "小R";
					}else if(userKind == "5"){// 买币蝗虫 绿色
						html += "<tr style='background-color:#008000;'>";
						typeStr = "买币蝗虫";
					}else if(userKind == "0" && score >= 2000000){// 超过200万的蝗虫 紫色
						html += "<tr style='background-color:#a800ff;'>";
						typeStr = "蝗虫玩家";
					}else if(userKind == "0" && zbl != 0){// 被控制的蝗虫 土色
						html += "<tr style='background-color:#ffcc00;'>";
						typeStr = "蝗虫玩家";
					}else if(userKind == "0"){// 蝗虫 无色
						html += "<tr>";
						typeStr = "蝗虫玩家";
					}else if(userKind == "7"){// 内部用户
						html += "<tr style='background-color:#6570fd;'>";
						typeStr = "内部用户";
					}else{
						html += "<tr>";
						typeStr = "蝗虫玩家";
					}
					html += "<td style='padding:5px;'><input type='checkbox' name='gameIds' value='"+datas[i].gameId+"' /></td>";
					html += "<td>"+datas[i].zx+"</td>";//在线房间名 如果为空 为大厅
					html += "<td><a style='font-weight:bold; color:black;' href='playerDetailsInfo?gameId="+datas[i].gameId+"' title='查看玩家详情'>"+datas[i].gameId+"</a></td>";//推广号
					html += "<td><a style='font-weight:bold; color:black;' href='playerDetailsInfo?gameId="+datas[i].gameId+"' title='查看玩家详情'>"+datas[i].nickName+"</a></td>";//用户昵称
					html += "<td>"+score+"</td>";//资产
					html += "<td>"+datas[i].prizes+"</td>";//奖券
					html += "<td>"+datas[i].cz+"</td>";//充值
					html += "<td>"+datas[i].ZengSongCha;+"</td>";//转账
					var regTime = datas[i].regTime;
					regTime = regTime.substring(0, regTime.lastIndexOf(":"));
					html += "<td>"+regTime+"</td>";//注册时间
					var lastLoginTime = datas[i].lastLoginTime;
					if(lastLoginTime!=null){
						lastLoginTime = lastLoginTime.substring(0, lastLoginTime.lastIndexOf(":"));
					}else{
						lastLoginTime="";
					}

					html += "<td>"+lastLoginTime+"</td>";//最后登陆时间
					html += "<td>"+datas[i].TodayScore+"</td>";//今日输赢
					html += "<td>"+realScore+"</td>";//总输赢

					//作弊控制（0：系统； 1：GM）
					var HandCheat = datas[i].HandCheat;
					if(HandCheat=="0"){
						HandCheat = "(系统)";
					}else if(HandCheat == "1"){
						HandCheat = "(GM)";
					}else if(HandCheat == "2"){
						HandCheat = "(循环)";
					}
					var pt = datas[i].platform;
					if(pt == "1"){
						pt = "PC";
					}else if(pt == "2"){
						pt = "IOS";
					}else if(pt == "3"){
						pt = "Android";
					}
					html += "<td>"+datas[i].cheatRate+"</td>";//作弊率 万分制
					html += "<td>"+datas[i].limitScore+"</td>";//作弊限额
					html += "<td>"+datas[i].bloodScore+"</td>";//作弊状态

					html += "<td>"+datas[i].pt+"</td>";//平台
					html += "<td>"+datas[i].platFormName+"</td>";//游戏平台
					html += "<td>"+typeStr+"</td>";//用户类型
					html += "</tr>";
				}
			}
			$("#databody").html(html);

			//分页数据
			var pageInfo = msg.pageInfo;
			//总条数
			var allCount = pageInfo.count;
			// alert(allCount);
			$("#count").html(allCount);
			//总页数
			var allPage = Math.ceil(allCount/pageSize);
			if(allPage==0){
				allPage = 1;
			}
			//当前页码
			var now = pageInfo.page;

			$("#page").val(now);
			$("#nowPage").html(now);
			$("#allPage").html(allPage);
			$("#pageIpu").val("");
		}
	});
	
}

//搜索玩家
function startSearch(){
	loadDatas('search');
	$("#searchModal").modal("hide");
}

//查询所有
function searchAll(s){
	$("#searchModal").modal("hide");
	$("#load").css("display","block");
	
	$(":reset").click();
	
	loadDatas(s);
}

function loadGamesKindName(){
	$.ajax({
		type: "post",
		url: "../../php/sys/roomSet/loadGames.php",
		success: function(msg){
			var datas = msg.records;
			
			var html = "<option value=''>全部</option>";
			if(datas.length!=0){
				for(var i = 0; i < datas.length; i++){
					html += "<option value='"+datas[i].kindName+"'>"+datas[i].kindName+"</option>";
				}
			}
			$("#game").html(html);
		}
	});
}

//全选及取消全选
function checkAll(obj){
	
	var flag = obj.checked;
	
	var CheckBox = $(":checkbox[name='gameIds']"); 
	if(flag){
		for(i = 0; i < CheckBox.length; i ++){
			CheckBox[i].checked = true;
		}
	}else{
		for(i = 0; i < CheckBox.length; i ++){
			CheckBox[i].checked = false;
		}
	}
}

//批量锁定用户
function batchLockUser(){
	var checks = $(":checkbox[name='gameIds']:checked");
	
	if(checks.length <= 0){
		$("#tishi").html("请选择要锁定的玩家！");
		$("#tishi_window").modal("show");
	}else{
		var gameIdStr = "";
		for(var i = 0; i < checks.length; i ++){
			if(gameIdStr != ""){
				gameIdStr += ",";
			}
		    if(checks[i].checked == true){
		    	gameIdStr += $(checks[i]).val();
			}
		}
		
		$("#gameIdStr").val(gameIdStr);
		$("#batchLockUserWindow").modal("show");
	}
}

//提交要锁定的玩家gameId
function sureLockGameIds(){
	//隐藏确认框
	$("#batchLockUserWindow").modal("hide");
	//显示填写备注的弹框
	$("#addLockRemarkWindow").modal("show");
}

function subLockGameIds(){
	var gameIdStr = $("#gameIdStr").val();
	
	var remark = $("#remark").val();
	
	if($.trim(remark) == ""){
		$("#remark").css("border","1px solid red");
		$("#lockError").css("display","block");
	}else{
		$("#remark").css("border","");
		$("#lockError").css("display","none");
		
		$.ajax({
			type: "post",
			data: {"type": 1, "remark": remark, "gameIdStr": gameIdStr},
			url: "/player/playerinfo/batchLockOrUnlockUser",
			beforeSend: function(){
				$("#remark, #gameIdStr").val("");
				$("#addLockRemarkWindow").modal("hide");
				$("#load").css("display","block");
			},
			success: function(msg){
				//console.log(msg);
				//return;
				$("#remark, #gameIdStr").val("");
				$("#addLockRemarkWindow").modal("hide");
				$("#load").css("display","none");
				
				if(msg == "1"){
					$("#tishi").html("操作成功！");
					$("#tishi_window").modal("show");
				}else{
					$("#tishi").html(msg);
					$("#tishi_window").modal("show");
				}
			}
		});
	}
	
}

//取消锁定
function cannelLockGameIds(){
	$("#remark").css("border","");
	$("#lockError").css("display","none");
	$("#remark, #gameIdStr").val("");
	$("#addLockRemarkWindow").modal("hide");
}



//批量解锁用户
function batchUnlockUser(){
	var checks = $(":checkbox[name='gameIds']:checked");
	
	if(checks.length <= 0){
		$("#tishi").html("请选择要解锁的玩家！");
		$("#tishi_window").modal("show");
	}else{
		var unlockGameIdStr = "";
		for(var i = 0; i < checks.length; i ++){
			if(unlockGameIdStr != ""){
				unlockGameIdStr += ",";
			}
		    if(checks[i].checked == true){
		    	unlockGameIdStr += $(checks[i]).val();
			}
		}
		
		$("#unlockGameIdStr").val(unlockGameIdStr);
		$("#batchUnlockUserWindow").modal("show");
	}
}

//提交批量解锁用户
function subBatchUnlockUsers(){
	var unlockGameIdStr = $("#unlockGameIdStr").val();
	
	var unlockRemark = $("#unlockRemark").val();
	
	if($.trim(unlockRemark) == ""){
		$("#unlockRemark").css("border","1px solid red");
		$("#unlockError").css("display","block");
	}else{
		$("#unlockRemark").css("border","");
		$("#unlockError").css("display","none");
		
		$.ajax({
			type: "post",
			data: {"type": 0, "remark": unlockRemark, "gameIdStr": unlockGameIdStr},
			url: "/player/playerinfo/batchLockOrUnlockUser",
			beforeSend: function(){
				$("#unlockRemark, #unlockGameIdStr").val("");
				$("#batchUnlockUserWindow").modal("hide");
				$("#load").css("display","block");
			},
			success: function(msg){
				
				$("#unlockRemark, #unlockGameIdStr").val("");
				$("#batchUnlockUserWindow").modal("hide");
				$("#load").css("display","none");
				
				if(msg == "1"){
					$("#tishi").html("操作成功！");
					$("#tishi_window").modal("show");
				}else{
					$("#tishi").html(msg);
					$("#tishi_window").modal("show");
				}
			}
		});
	}	
}

//取消批量解锁
function cannelUnlockGameIds(){
	$("#unlockGameIdStr, #unlockRemark").val("");
	
	$("#unlockError").css("display","none");
	
	$("#unlockRemark").css("border","");
	
	$("#batchUnlockUserWindow").modal("hide");
}
